<%--
  -	Date: 2012-03-27
  -
  - Copyright (C) 2012 Jawa Software. All rights reserved.
  - 
  - 系统集群管理页面。
--%>
<%@ page language="java" pageEncoding="utf-8"%>

<%@ page import="java.text.DecimalFormat"%>
<%@ page import="java.util.*"%>
<%@ page import="org.jawa.admin.util.ParamUtils"%>
<%@ page import="org.jawa.core.cluster.ClusterManager"%>
<%@ page import="org.jawa.core.log.Log"%>
<%@ page import="org.jawa.core.cluster.ClusterNodeInfo"%>
<%@ page import="org.jawa.core.cache.CacheFactory"%>
<%@ page import="org.jawa.core.cluster.GetBasicStatistics"%>
<%@ page import="org.jawa.core.JawaServer"%>
<%@ page import="org.jawa.core.util.JawaGlobals"%>

<jsp:useBean id="webManager" class="org.jawa.admin.util.WebManager" />
<%
	webManager.init(request, response, session, application, out);
	String path = request.getContextPath();
%>

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<title>集群管理</title>
		<meta name="pageID" content="1104" />
	</head>
	<%
		boolean update = request.getParameter("openClustering") != null
				|| request.getParameter("closeClustering") != null;
		boolean clusteringEnabled = request.getParameter("openClustering") != null;
		boolean updateSucess = false;

		if (update) {
			if (!clusteringEnabled) {
				ClusterManager.setClusteringEnabled(false);
				webManager.logEvent("关闭集群","");
				updateSucess = true;
			} else {
				ClusterManager.setClusteringEnabled(true);
				webManager.logEvent("打开集群","");
				updateSucess = ClusterManager.isClusteringStarted();
			}
		}

		boolean clusteringStarting = ClusterManager.isClusteringStarting();
		boolean clusteringStarted = ClusterManager.isClusteringStarted();
		int maxClusterNodes = ClusterManager.getMaxClusterNodes();
		clusteringEnabled = ClusterManager.isClusteringStarted() || ClusterManager.isClusteringStarting();

		Collection<ClusterNodeInfo> clusterNodesInfo = ClusterManager.getNodesInfo();
		
		Collection<Object> statistics = CacheFactory.doSynchronousClusterTask(new GetBasicStatistics(), true);
		
	%>
	<body>
		<div class="row-fluid">
			<ul class="breadcrumb">
				<li>
					您现在的位置:
				</li>
				<li>
					<a href="<%=path%>/admin/index.jsp">服务器</a>
				</li>
				<li>
					<span class="divider">/</span>
				</li>
				<li class="active">
					集群管理
				</li>
			</ul>
			<%
				if (update) {
					if (updateSucess) {
			%>
			<div class="alert alert-success fade in">
				<a class="close" data-dismiss="alert" href="#">×</a>
				<i class="icon-ok-circle icon-white"></i><strong>操作集群成功!</strong>
			</div>
			<%
				} else {
			%>
			<div class="alert alert-error fade in">
				<a class="close" data-dismiss="alert" href="#">×</a>
				<i class="icon-remove-circle icon-white "></i><strong>启动集群出错!</strong>
			</div>
			<%
				}
				}
			%>
			<div>
				<form action="<%=path%>/admin/system-clustering.jsp" method="post"
					name="cacheForm " class="well">
					<div>
						<button id="openBtn" name="openClustering" type="submit"
							class="btn btn-primary"
							<%=(clusteringStarting || clusteringStarted ? "disabled=\"disabled\" " : "")%>>
							打开集群
						</button>
						<button id="closeBtn" name="closeClustering" type="submit"
							class="btn btn-primary"
							<%=(!(clusteringStarting || clusteringStarted) ? "disabled=\"disabled\" " : "")%>>
							关闭集群
						</button>
					</div>
				</form>
			</div>
			
			<div class="well">
				<table class="table table-striped  ">
					<thead>
						<tr>
							<th>
								集群成员
							</th>
							<th>
								加入时间
							</th>
							<th>
								使用空间
							</th>
						</tr>
					</thead>
					<tbody>
						<%
							if (!clusterNodesInfo.isEmpty()) {
								int i = 1;
								for (ClusterNodeInfo nodeInfo : clusterNodesInfo) {
									boolean isLocalMember = JawaServer.getInstance().getHostName().equals(nodeInfo.getMemberName());
									Map<String, Object> nodeStats = null;
									for (Object stat : statistics) {
										Map<String, Object> statsMap = (Map<String, Object>) stat;
										if (statsMap == null) {
											continue;
										}
										//if (Arrays.equals((byte[]) statsMap.get(GetBasicStatistics.NODE), nodeInfo.getMemberName().getBytes())) {
										if (((String)statsMap.get(GetBasicStatistics.NODE)).equals(nodeInfo.getMemberName())) {
											nodeStats = statsMap;
											break;
										}
									}
									
									int percent = 0;
									String memory = "N/A";
									if (nodeStats != null) {
										double usedMemory = (Double) nodeStats.get(GetBasicStatistics.MEMORY_CURRENT);
										double maxMemory = (Double) nodeStats.get(GetBasicStatistics.MEMORY_MAX);
										double percentFree = ((maxMemory - usedMemory) / maxMemory) * 100.0;
										percent = 100 - (int) Math.round(percentFree);
										DecimalFormat mbFormat = new DecimalFormat("#0.00");
										memory = ""+mbFormat.format(usedMemory) + " MB (共" + mbFormat.format(maxMemory) + " MB) ";
									}
									
						%>
						<tr>
							<td>
								<strong> 
								 <%
								 	if (isLocalMember) {
								 %> 
								           本机
								 <%
								 	} else {
								 %>
								 <%=nodeInfo.getHostName()%> 
								 <%
								 	}
								 %> 
								</strong>
							</td>
							<td><%=JawaGlobals.formatDateTime((new Date(nodeInfo.getJoinedTime())))%></td>
							<td><%=memory%></td>
						</tr>
						<%
							}
						 }
						%>
					</tbody>
				</table>
			</div>
		</div>

	</body>
</html>
